package com.rytx.web.controller.ysp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.rytx.common.core.domain.entity.SysDictData;
import com.rytx.common.core.domain.model.LoginUser;
import com.rytx.common.utils.StringUtils;
import com.rytx.framework.web.service.TokenService;
import com.rytx.system.domain.ArtKd100;
import com.rytx.system.domain.ysp.ArtMessage;
import com.rytx.system.domain.ysp.ArtOrder;
import com.rytx.system.domain.ysp.ArtRental;
import com.rytx.system.service.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.rytx.common.annotation.Log;
import com.rytx.common.core.controller.BaseController;
import com.rytx.common.core.domain.AjaxResult;
import com.rytx.common.enums.BusinessType;
import com.rytx.system.domain.ArtOrderAfter;
import com.rytx.common.utils.poi.ExcelUtil;
import com.rytx.common.core.page.TableDataInfo;

/**
 * 售后订单Controller
 *
 * @author rytx
 * @date 2024-08-29
 */
@RestController
@RequestMapping("/system/orderAfter")
public class ArtOrderAfterController extends BaseController
{
    @Autowired
    private IArtOrderAfterService artOrderAfterService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Resource
    private IArtKd100Service artKd100Service;
    @Autowired
    private IArtOrderService artOrderService;//订单表
    @Resource
    private IArtRentalService artRentalService;

    /**
     * 撤销售后
     * */
    @ApiOperation(
            value = "撤销售后",
            notes = "撤销售后",
            response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="platOrderNo",value="系统订单号",required=true)
    })
    @PostMapping("/appCancelOrderAfter")
    public AjaxResult appCancelOrderAfter(ArtOrderAfter artOrderAfter, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        artOrderAfter.setUserId(userId);
        return AjaxResult.success(artOrderAfterService.cancelOrderAfter(artOrderAfter));
    }

    @ApiOperation(
            value = "售后列表",
            notes = "售后列表",
            response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="afterType",value="售后类型 1=退货退款，2=归还，3=提前归还 4 =仅退款"),
    })
    @GetMapping("/appList")
    public TableDataInfo appList(ArtOrderAfter artOrderAfter, HttpServletRequest request)
    {
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        artOrderAfter.setUserId(userId);

        startPage();
        List<ArtOrderAfter> list = artOrderAfterService.selectArtOrderAfterListByAfterType(artOrderAfter);
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (ArtOrderAfter after : list) {
            ArtOrder order = artOrderService.getOrderByOrderNo(after.getPlatOrderNo());
            ArtRental artRental = artRentalService.selectArtRentalById(order.getArtRentalId());
            Map<String,Object> map = new HashMap<>();
            map.put("order",order);
            map.put("artRental",artRental);
            map.put("after",after);
            mapList.add(map);
        }
        return getDataTable(mapList);
    }

    /**
     * 申请售后
     * */
    @ApiOperation(
            value = "申请售后",
            notes = "申请售后",
            response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="platOrderNo",value="系统订单号",required=true),
            @ApiImplicitParam(name="afterType",value="售后类型 1=退货退款，2=归还，3=提前归还 4 =仅退款",required=true),
            @ApiImplicitParam(name="explain",value="售后原因",required=true),
    })
    @PostMapping("/createOrderAfter")
    public AjaxResult createOrderAfter(ArtOrderAfter artOrderAfter, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        artOrderAfter.setUserId(userId);
        artOrderAfterService.createOrderAfter(artOrderAfter);
        return AjaxResult.success();
    }

    /**
     * 查询申请售后信息
     * */
    @ApiOperation(
            value = "查询申请售后信息",
            notes = "查询申请售后信息",
            response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="orderNo",value="系统订单号",required=true),
    })
    @GetMapping("/getOrderAfterInfo")
    public AjaxResult getOrderAfterInfo(String orderNo){
        ArtOrder order = artOrderService.getOrderByOrderNo(orderNo);
        List<ArtRental> artRental = artRentalService.selectArtRentalByIds(order.getCartRentalIds());
        ArtOrderAfter artOrderAfter =   artOrderAfterService.getOrderAfterInfo(orderNo);
        return AjaxResult.success().put("after",artOrderAfter).put("order",order).put("rentalList",artRental);
    }
    /**
     *修改售后订单的运单号、快递公司、运单图片
     * */
    @ApiOperation(
            value = "修改售后订单的运单号、快递公司、运单图片",
            notes = "修改售后订单的运单号、快递公司、运单图片",
            response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="platAfterNo",value="售后订单号",required=true),
            @ApiImplicitParam(name="com",value="快递公司编号",required=true),
            @ApiImplicitParam(name="nu",value="运单号",required=true),
            @ApiImplicitParam(name="logisticsUrl",value="物流运单图片",required=true),
    })
    @PostMapping("/updateOrderAfterLogisticsInfo")
    public AjaxResult updateOrderAfterLogisticsInfo(ArtOrderAfter artOrderAfter, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        artOrderAfter.setUserId(userId);
        artOrderAfterService.updateOrderAfterLogisticsInfo(artOrderAfter);
        return AjaxResult.success();
    }
    /**
     * 查询快递公司键值对
     * */
    @ApiOperation(
            value = "查询快递公司键值对",
            notes = "查询快递公司键值对",
            response = AjaxResult.class)
    @GetMapping("/getExpressCompany")
    public AjaxResult getExpressCompany(){

        List<SysDictData> data = dictTypeService.selectDictDataByType("courier_company");
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }
    /**
     *取消售后
     * */
    @ApiOperation(
            value = "取消售后",
            notes = "取消售后",
            response = AjaxResult.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name="platAfterNo",value="售后订单号",required=true),

    })
    @PostMapping("/cancelOrderAfter")
    public AjaxResult cancelOrderAfter(@RequestBody ArtOrderAfter artOrderAfter, HttpServletRequest request){
        LoginUser loginUser = tokenService.getLoginUser(request);
        Long userId = loginUser.getUserId();
        artOrderAfter.setUserId(userId);//这里得到的是后管中的用户id . 但是如果是小程序用户调用则是取得小程序用户的id
        artOrderAfter.setStatus(6l);

        return artOrderAfterService.updateOrderAfterStatus(artOrderAfter);
    }
   /**
    * 修改售后状态 审核通过 驳回 完成 后台管理系统用的 todo
    * */
   @ApiOperation(
           value = "修改售后状态 审核通过、驳回、完成 后台管理系统用的",
           notes = "修改售后状态 审核通过、驳回、完成 后台管理系统用的",
           response = AjaxResult.class)
   @ApiImplicitParams({
           @ApiImplicitParam(name="platAfterNo",value="售后订单号",required=true),
           @ApiImplicitParam(name="status",value="状态1=待审核,2=售后中,3=已完成,4=已关闭5=审核驳回6=已取消",required=true),
           @ApiImplicitParam(name="opinion",value="审核意见",required=false),
           @ApiImplicitParam(name="compensationAmount",value="客户补偿金额",required=false),
           //赔偿金额
   })
   @PostMapping("/updateOrderAfterStatus")
   public AjaxResult updateOrderAfterStatus(@RequestBody ArtOrderAfter artOrderAfter,@RequestBody ArtMessage artMessage, HttpServletRequest request){
       LoginUser loginUser = tokenService.getLoginUser(request);
       Long userId = loginUser.getUserId();
       artOrderAfter.setUserId(userId);//这里得到的是后管中的用户id . 但是如果是小程序用户调用则是取得小程序用户的id
       artOrderAfterService.updateOrdermessageAfterStatus(artOrderAfter,artMessage);
       return AjaxResult.success();
   }
   /**
    *查看物流信息  todo
    * */
   @ApiOperation(
           value = "查看物流信息",
           notes = "查看物流信息",
           response = AjaxResult.class)
   @GetMapping("/getLogisticsInfo")
   public AjaxResult getLogisticsInfo(String nu,String com){
       ArtKd100 kd100ByNo = artKd100Service.getKd100ByNo(nu, com);
       return success(kd100ByNo);
   }


    /**
     * 查询售后订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderAfter:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArtOrderAfter artOrderAfter)
    {
        startPage();
        List<ArtOrderAfter> list = artOrderAfterService.selectArtOrderAfterList(artOrderAfter);
        return getDataTable(list);
    }

    /**
     * 导出售后订单列表
     */
    @PreAuthorize("@ss.hasPermi('system:orderAfter:export')")
    @Log(title = "售后订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArtOrderAfter artOrderAfter)
    {
        List<ArtOrderAfter> list = artOrderAfterService.selectArtOrderAfterList(artOrderAfter);
        ExcelUtil<ArtOrderAfter> util = new ExcelUtil<ArtOrderAfter>(ArtOrderAfter.class);
        util.exportExcel(response, list, "售后订单数据");
    }

    /**
     * 获取售后订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:orderAfter:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(artOrderAfterService.selectArtOrderAfterById(id));
    }

    /**
     * 新增售后订单
     */
    @PreAuthorize("@ss.hasPermi('system:orderAfter:add')")
    @Log(title = "售后订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArtOrderAfter artOrderAfter)
    {
        return toAjax(artOrderAfterService.insertArtOrderAfter(artOrderAfter));
    }

    /**
     * 修改售后订单
     */
    @PreAuthorize("@ss.hasPermi('system:orderAfter:edit')")
    @Log(title = "售后订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArtOrderAfter artOrderAfter)
    {
        return toAjax(artOrderAfterService.updateArtOrderAfter(artOrderAfter));
    }

    /**
     * 删除售后订单
     */
    @PreAuthorize("@ss.hasPermi('system:orderAfter:remove')")
    @Log(title = "售后订单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(artOrderAfterService.deleteArtOrderAfterByIds(ids));
    }
}
